package com.lwl.blue.controller;

import com.lwl.blue.commont.utils.RedisUtil;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.concurrent.TimeUnit;

/**
 * Redis 操作示例控制器
 */
@RestController
@RequestMapping("/redis")
public class RedisController {

    @Resource
    private RedisUtil redisUtil;

    /**
     * 测试 String 类型操作
     */
    @GetMapping("/string")
    public String testString() {
        // 设置值并过期
        redisUtil.set("test:name", "RedisDemo", 10, TimeUnit.MINUTES);
        // 获取值
        Object name = redisUtil.get("test:name");
        // 自增
        redisUtil.set("test:count", 0);
        redisUtil.increment("test:count", 1);
        return "String 操作成功，name=" + name + ", count=" + redisUtil.get("test:count");
    }

    /**
     * 测试 Hash 类型操作（模拟存储用户信息）
     */
    @GetMapping("/hash")
    public String testHash() {
        String key = "user:100";
        redisUtil.hSet(key, "id", 100);
        redisUtil.hSet(key, "name", "张三");
        redisUtil.hSet(key, "age", 25);
        // 获取用户信息
        Object userName = redisUtil.hGet(key, "name");
        return "Hash 操作成功，用户名=" + userName;
    }

    /**
     * 测试删除操作
     */
    @DeleteMapping("/{key}")
    public String deleteKey(@PathVariable String key) {
        boolean result = redisUtil.delete(key);
        return result ? "删除成功" : "删除失败（键不存在）";
    }
}
